﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MobileDao
/// </summary>
public class MobileDao
{
    #region "variable"
    private MobileDto dto;
    private string _error;
    private MyConnection _myConn;
    #endregion

    #region "properties"
    public string Error
    {
        get { return _error; }
        set { _error = value; }
    }
    public MobileDto Dto
    {
        get { return dto; }
        set { dto = value; }
    }
    #endregion
	public MobileDao()
	{
        _myConn = new MyConnection();
        Dto = new MobileDto();
        Error = "";
	}
    public void DeleteMobileNumber(int id_MN)
    {
        string spName = "sp_Delete_MobileNumber";
        SqlParameter[] parameter = { new SqlParameter("@id_MN", SqlDbType.Int) };
        parameter[0].Value = id_MN;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public MobileDto SelectMobileByNumber(string phone)
    {
        string spName = "sp_Select_MobileByNumber";
        SqlParameter[] parameter = { new SqlParameter("@_phone", SqlDbType.NVarChar, 20) };
        parameter[0].Value = phone;
        DataTable table = _myConn.GetDataTable(spName, parameter);
        if (table.Rows.Count > 0)
            return new MobileDto(table.Rows[0]);
        else
        {
            MobileDto dto = new MobileDto();
            dto.Id_MN = -1;
            return dto;
        }                
    }
    public List<MobileDto> SelectMobileByID_UA(int id_UA)
    {
        string spName = "sp_Select_MobileByID_UA";
        SqlParameter[] parameter = { new SqlParameter("@id_UA",SqlDbType.Int)};
        parameter[0].Value = id_UA;
        DataTable table = _myConn.GetDataTable(spName, parameter);
        return this.MakeListBillDto(table);
    }

    public List<MobileDto> MakeListBillDto(DataTable table)
    {
        List<MobileDto> list = new List<MobileDto>();
        foreach (DataRow row in table.Rows)
        {
            list.Add(new MobileDto(row));
        }
        return list;
    }
}